/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bo;

import dao.DepartmentDAO;
import dao.DesignationDAO;
import dao.EmployeeDAO;
import dao.SectionDAO;
import dao.VacanciesDAO;
import dao.VacancyfilldetailsDAO;
import data.Departments;
import data.Employees;
import data.Vacancies;
import data.Vacancyfilldetails;
import dialogview.FillVacancyDialog;
import dialogview.RotateEFillVacancy;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import javax.swing.table.TableModel;
import view.ManageVacancy;

/**
 *
 * @author Hùng
 */
public class VacancyfilldetailsBO {

    private String Success;
    private Pattern pattern;
    private Matcher matcher;

    public String getSuccess() {
        return Success;
    }

    public void setSuccess(String Success) {
        this.Success = Success;
    }

    public VacancyfilldetailsBO(String Success) {
        this.Success = Success;
    }

    public VacancyfilldetailsBO() {
    }

    public boolean validate(final String email) {
        pattern = Pattern.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
        matcher = pattern.matcher(email);
        return matcher.matches();

    }

    public List<Vacancyfilldetails> showInforVacDetailbyIdVac(String IdVac) {
        List<Vacancyfilldetails> lVFD = new VacancyfilldetailsDAO().getAllVacFilldetail(IdVac);
        return lVFD;
    }

    public void showInforOfVac(int[] selectedRows, TableModel model) {
        if (selectedRows.length == 1) {
            String tmpIdVac = (String) model.getValueAt(selectedRows[0], 0);
            Vacancies tmpVac = new VacanciesDAO().getVacancybyId(tmpIdVac);
            ManageVacancy.setsVacancies(tmpVac);
        } else {
            throw new Error("Please,Select only Vacancy on Table to Fill");
        }

    }

    public void fillVacancybyNewEmployee(String deptName, String SecName, String design, String userName, char[] password, Object tmpRole, String nameE, String addressE, String phoneE, String faxE, String emailE) {
        //check infor
        if (!userName.equals("") && password.length > 5 && tmpRole != null && !addressE.equals("") && !phoneE.equals("") && !faxE.equals("") && !emailE.equals("")) {
            //check email
            if (validate(emailE)) {
                //check username
                Employees tmpE = null;
                tmpE = new EmployeeDAO().getEbyUserName(userName);
                if (tmpE == null) {
                    Departments tmpDept = new DepartmentDAO().getDepartmentByName(deptName);
                    String passE = new String(password);
                    int Role = 0;
                    switch ((String) tmpRole) {
                        case "Admin":
                            Role = 1;
                            break;
                        case "Hrmanager":
                            Role = 2;
                            break;
                        case "Employee":
                            Role = 3;
                            break;
                    }
                    Employees tmpEmloyee = new Employees(
                            "E" + Calendar.getInstance().getTimeInMillis(),
                            new DesignationDAO().getDesignationbyDesignation(design),
                            new SectionDAO().getSectionbyNameSec(SecName),
                            nameE,
                            tmpDept.getDeptno(),
                            addressE,
                            phoneE,
                            faxE,
                            emailE,
                            userName,
                            passE,
                            Role, null, null);
                    Success = "Register Employee SuccessFully";
                    FillVacancyDialog.setsEmp(tmpEmloyee);
                } else {
                    throw new Error("Please,Your UserName is existed");
                }
            } else {
                throw new Error("Please,check your Email");
            }
        } else {
            throw new Error("Please,check your information");
        }
    }

    public void rotateEmployee(String idE) {
        if (!idE.equals("")) {
            Employees tmpE = new EmployeeDAO().findEmpbyIdE(idE);
            if (tmpE != null) {
                RotateEFillVacancy.setsREmp(tmpE);
            } else {
                throw new Error("Please,check your Id Employee");
            }
        } else {
            throw new Error("Please,Sreach Employee by Id Employee");
        }
    }

    public void registerFillDetailOfVacancy(String IdVac, String IdE, Date tmpDate, String takedetail) {
        if (!IdVac.equals("")) {
            if (!IdE.equals("")) {
                String tmpId1 = FillVacancyDialog.getsEmp().getEmpid();
                //new Emp
                if(tmpId1.equals(IdE)){
                    boolean flag = new EmployeeDAO().executeInsertHQLQuery(IdE);
                    if(flag){
                        Vacancies tmpV = new VacanciesDAO().getVacancybyId(IdVac);
//                        Vacancyfilldetails tmpVFD = new Vacancyfilldetails(
//                                IdE,
//                                null,
//                                tmpV, 
//                                takedetail,
//                                takedetail);
                    }else{
                        throw new Error("Register VacanyFillDetail Fail.Please,check Employee Id");
                    }
                }
            } else {
                throw new Error("Please,Check Id Employee ");
            }
        } else {
            throw new Error("Please,Select only Vacancy on Table to Fill");
        }
    }
}
